#include "CExplodingEnemy.h"

#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../Managers/VIN_ObjectManager.h"

//#include "../Game Objects/CEnemy.h"

#include "../Core/MessageSystem.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../Game States/CGamePlayState.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../Managers/VIN_SoundManager.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../Core/VIN_EventSystem.h"
#include "../tinyxml/tinyxml.h"

enum EAnimationState { ENEMY_IDLE, ENEMY_EXPLODING };

CExplodingEnemy::CExplodingEnemy()
{
	lifeTime = 20.0f;
	color = -1;
	SetAlive(true);
	SetPassive(false);
	SetDistanceToPlayer(Vector2D(0.0f, 0.0f));
	SetHP(70);
	SetPower(20);
	SetWidth(20);
	SetHeight(40);

	Load("resources/XML/BombGuy_Animation.xml");

	SetType(OBJ_ENEMY);

	scale = 1.0f;

	CurrAnimation = ENEMY_IDLE;
}

CExplodingEnemy::~CExplodingEnemy()
{
	if(!vAnimation.empty())
	{
		vAnimation.clear();
	}
}

RECT CExplodingEnemy::GetHealthRect()
{
	RECT temp;
	if(CurrAnimation < (int)vAnimation.size())
	{
		temp.left  = (int)GetPosX() + vAnimation[CurrAnimation].GetHealthFrame().left ;
		temp.right = (int)GetPosX() + vAnimation[CurrAnimation].GetHealthFrame().right;
		temp.top   = (int)GetPosY() + vAnimation[CurrAnimation].GetHealthFrame().top;
		temp.bottom= (int)GetPosY() + vAnimation[CurrAnimation].GetHealthFrame().bottom;

		return temp;
	}
	else
	{
		SetRect(&temp,0,0,0,0);
		return temp;
	}
}

void CExplodingEnemy::HandleEVent(CEvent* pEvent)
{

}

bool CExplodingEnemy::CheckCollision(IBaseInterface *pBase)
{
	RECT TempRect;

	if( pBase->GetType() == OBJ_ENEMY )
	{	
		CBaseEnemy* enemy = (CBaseEnemy*)pBase;

		if(CurrAnimation == ENEMY_EXPLODING)
		{
			if(IntersectRect(&TempRect, &GetHealthRect(), &enemy->GetHealthRect() ) )
			{		
				enemy->SetHP(0.0f);
				//CurrAnimation = ENEMY_EXPLODING;
				return true;
			}
		}
	}
	else if(CBaseEnemy::CheckCollision(pBase))
	{
		CurrAnimation = ENEMY_EXPLODING;
		color = -1;
		return true;
	}
	return false;
}

void CExplodingEnemy::Update( float fElapsedTime )
{
	lifeTime -= fElapsedTime;

	if(lifeTime < 0)
	{
		CurrAnimation = ENEMY_EXPLODING;
	}

	CBaseEnemy::Update(fElapsedTime);

	if(abs(GetPosX() - CGamePlayState::GetInstance()->GetPlayer()->GetPosX()) <= 20)
	{
		SetVelX(0);
		CGamePlayState::GetInstance()->GetPlayer()->SetHP(CGamePlayState::GetInstance()->GetPlayer()->GetHP() - (float)GetPower());
		CurrAnimation = ENEMY_EXPLODING;
		//VIN_SoundManager::GetInstance()->PlayPunch();
	}
	else
	{
		SetVelX(GetSpeed());
		//CurrAnimation = ENEMY_IDLE;
	}

	vAnimation[CurrAnimation].Update(fElapsedTime);
	
	vAnimation[CurrAnimation].Play();

	if(CurrAnimation == ENEMY_EXPLODING)
	{
		color = -1;

		SetVelX(0.0f);
		SetVelY(0.0f);
		//VIN_SoundManager::GetInstance()->PlayExplosion();
		//VIN_SoundManager::GetInstance()->PlayExplosion();
		//VIN_SoundManager::GetInstance()->PlayExplosion();
		//VIN_SoundManager::GetInstance()->PlayExplosion();
		//VIN_SoundManager::GetInstance()->PlayExplosion();

		if(vAnimation[CurrAnimation].GetCurFrame() == vAnimation[CurrAnimation].GetMaxFrames() - 1)
		{
			SetHP(0.0f);
			//CGamePlayState::GetInstance()->GetPlayer()->SetHP(CGamePlayState::GetInstance()->GetPlayer()->GetHP() - (float)GetPower());
			VIN_SoundManager::GetInstance()->PlayExplosion();
		}
	}
}

void CExplodingEnemy::Render(void)
{
	CSGD_TextureManager* pTM  = CSGD_TextureManager::GetInstance();
	CSGD_Direct3D* d3d  = CSGD_Direct3D::GetInstance();

	vAnimation[CurrAnimation].Render(GetPosX() , GetPosY() , true, scale, color);

	d3d->GetSprite()->Flush();

	//d3d->DrawRect(GetHealthRect(), 255, 0, 0);
	
}

bool CExplodingEnemy::Load( const char* AnimInfo)
{
	int tempID;

	TiXmlDocument doc;

	if(doc.LoadFile(AnimInfo) == false)
		return false;

	TiXmlElement* Root = doc.RootElement();
	if(Root == NULL)
		return false;

	TiXmlElement* NumAnim = Root->FirstChildElement("xNumOfAnim");
	int count = atoi(NumAnim->GetText());

	TiXmlElement* ImageFile = Root->FirstChildElement("xImageFile");
	if(ImageFile)
	{
		tempID = CSGD_TextureManager::GetInstance()->LoadTexture(ImageFile->GetText());
	}

	TiXmlElement* xAnimation  = Root->FirstChildElement("xAnimation");

	while(xAnimation)
	{
		CAnimation a;

		TiXmlElement* NumFrame = xAnimation->FirstChildElement("xNumOfFrames");
		int fcount = atoi(NumFrame->GetText());

		TiXmlElement* looping = xAnimation->FirstChildElement("xLooping");
		a.SetIsLooping((bool)(atoi(looping->GetText())));

		TiXmlElement* AnimSpd = xAnimation->FirstChildElement("xAnimSpd");
		a.SetSpeed((float)( atoi(AnimSpd->GetText())));

		TiXmlElement* xFrame = xAnimation->FirstChildElement("xFrame");

		Frame temp;
		temp.fTime = 0;
		temp.ptAnchor.x = 0;
		temp.ptAnchor.y = 0;

		SetRect(&temp.rAttack,0,0,0,0);
		SetRect(&temp.rCollision,0,0,0,0);
		SetRect(&temp.rRender,0,0,0,0);

		temp.ptAnchor.x = 0;
		temp.ptAnchor.y = 0;


		while(xFrame)
		{
			TiXmlElement* xTime = xFrame->FirstChildElement("xTime");
			if(xTime)
			{
				temp.fTime =(float)( atoi(xTime->GetText()) );
			}
			TiXmlElement* FrameType = xFrame->FirstChildElement("xFrameType");
			if(FrameType)
			{
				TiXmlElement* RenderFrame = FrameType->FirstChildElement("xRenderFrame");
				if(RenderFrame)
				{
					TiXmlElement* RenderFrameX = RenderFrame->FirstChildElement("xRenderFrameX");
					if(RenderFrameX)
						temp.rRender.left =  atoi(RenderFrameX->GetText());

					TiXmlElement* RenderFrameY = RenderFrame->FirstChildElement("xRenderFrameY");
					if(RenderFrameY)
						temp.rRender.top = atoi(RenderFrameY->GetText());

					TiXmlElement* RenderFrameW = RenderFrame->FirstChildElement("xRenderFrameW");
					if(RenderFrameW)
					{
						temp.rRender.right = atoi(RenderFrameW->GetText());
						temp.rRender.right += temp.rRender.left;
					}

					TiXmlElement* RenderFrameH = RenderFrame->FirstChildElement("xRenderFrameH");
					if(RenderFrameH)
					{
						temp.rRender.bottom = atoi(RenderFrameH->GetText());
						temp.rRender.bottom += temp.rRender.top;
					}
				}

				TiXmlElement* CollisionFrame = FrameType->FirstChildElement("xCollisionFrame");
				if(CollisionFrame)
				{
					TiXmlElement* CollisionFrameX = CollisionFrame->FirstChildElement("xCollisionFrameX");
					if(CollisionFrameX)
						temp.rCollision.left = atoi(CollisionFrameX->GetText());

					TiXmlElement* CollisionFrameY = CollisionFrame->FirstChildElement("xCollisionFrameY");
					if(CollisionFrameY)
						temp.rCollision.top = atoi(CollisionFrameY->GetText());

					TiXmlElement* CollisionFrameW = CollisionFrame->FirstChildElement("xCollisionFrameW");
					if(CollisionFrameW)
						temp.rCollision.right = atoi(CollisionFrameW->GetText());

					TiXmlElement* CollisionFrameH = CollisionFrame->FirstChildElement("xCollsionFrameH");
					if(CollisionFrameH)
						temp.rCollision.bottom = atoi(CollisionFrameH->GetText());
				}

				TiXmlElement* AttackFrame = FrameType->FirstChildElement("xAttackFrame");
				if(AttackFrame)
				{
					TiXmlElement* AttackFrameX = AttackFrame->FirstChildElement("xAttackFrameX");
					if(AttackFrameX)
						temp.rAttack.left = atoi(AttackFrameX->GetText());

					TiXmlElement* AttackFrameY = AttackFrame->FirstChildElement("xAttackFrameY");
					if(AttackFrameY)
						temp.rAttack.top = atoi(AttackFrameY->GetText());

					TiXmlElement* AttackFrameW = AttackFrame->FirstChildElement("xAttackFrameW");
					if(AttackFrameW)
						temp.rAttack.right = atoi(AttackFrameW->GetText());

					TiXmlElement* AttackFrameH = AttackFrame->FirstChildElement("xAttackFrameH");
					if(AttackFrameH)
						temp.rAttack.bottom = atoi(AttackFrameH->GetText());
				}

				TiXmlElement* AnchorPoint = FrameType->FirstChildElement("xAnchorPoint");
				if(AnchorPoint)
				{
					TiXmlElement* AnchorPointX = AnchorPoint->FirstChildElement("xAnchorPointX");
					if(AnchorPointX)
					{
						int ax = atoi(AnchorPointX->GetText());
						temp.ptAnchor.x =  ax - temp.rRender.left;
					}

					TiXmlElement* AnchorPointY = AnchorPoint->FirstChildElement("xAnchorPointY");
					if(AnchorPointY)
					{
						int ay = atoi(AnchorPointY->GetText());
						temp.ptAnchor.y = ay - temp.rRender.top;
					}
				}
			}
			a.SetTimeDelay(temp.fTime);

			temp.rAttack.right +=  temp.rAttack.left;
			temp.rAttack.bottom += temp.rAttack.top;
			temp.rAttack.left -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rAttack.top -= (temp.ptAnchor.y + temp.rRender.top);
			temp.rAttack.right -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rAttack.bottom -=(temp.ptAnchor.y + temp.rRender.top);



			temp.rCollision.right +=  temp.rCollision.left;
			temp.rCollision.bottom += temp.rCollision.top;
			temp.rCollision.left -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rCollision.top -= (temp.ptAnchor.y + temp.rRender.top);
			temp.rCollision.right -= (temp.ptAnchor.x + temp.rRender.left);
			temp.rCollision.bottom -=(temp.ptAnchor.y + temp.rRender.top);


			a.addFrame(temp);
			xFrame = xFrame->NextSiblingElement("xFrame");
		}
		a.SetImageID(tempID);
		vAnimation.push_back(a);

		xAnimation = xAnimation->NextSiblingElement("xAnimation");



	}



	return true;

}
